package com.info3k.client.components;

import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.MenuBar;
import com.google.gwt.user.client.ui.MenuItem;
import com.info3k.client.Info3K;
import com.info3k.client.commands.CommandHandler;
import com.info3k.client.commands.MainCommand;
import com.info3k.client.commands.NewPageCommand;
import com.info3k.client.enums.UserTypes;

public class MenuManager extends Composite {
	
	private Info3K info3k;
	
    private MenuBar menuBar = new MenuBar();
    
	private FlowPanel panel = new FlowPanel();
	
	public MenuManager(Info3K info3k){	        
	 	this.info3k = info3k;
        initWidget(panel);
	 	panel.setStylePrimaryName("mo_menu");
	 	renderMenus();
	}
	
	public void renderMenus(){
		panel.remove(menuBar);
		menuBar = new MenuBar();
		menuBar.addStyleName("info3k-MenuBar");
		menuBar.setAnimationEnabled(true);
		
		MenuItem eventsListItem = new MenuItem(Info3K.msgs.menuHome(), new CommandHandler("latestEvents", info3k));
		menuBar.addItem(eventsListItem);
		
		final MenuBar menuInfo3K = new MenuBar(true);
		
		menuInfo3K.addItem(Info3K.msgs.aboutProject(), new MainCommand("../pages/main.html"));
		menuInfo3K.addItem(Info3K.msgs.issueTracker(), new NewPageCommand("http://code.google.com/p/3k-info/issues/list"));
		menuInfo3K.setAnimationEnabled(true);
		
		final MenuBar menuLocations = new MenuBar(true);
		final MenuBar menuAddresses = new MenuBar(true);
		
		MenuItem locationListItem = new MenuItem(Info3K.msgs.locationList(), new CommandHandler("locationList", info3k));
		MenuItem addressListItem = new MenuItem(Info3K.msgs.addressList(), new CommandHandler("addressList", info3k));
		MenuItem addNewLocationItem = new MenuItem(Info3K.msgs.addNewLocation(), new CommandHandler("addNewLocation", info3k));
		MenuItem addnewAddressItem = new MenuItem(Info3K.msgs.addNewAddress(), new CommandHandler("addNewAddress", info3k));
		
		MenuItem addNewCategoryItem = new MenuItem(Info3K.msgs.addNewCategory(), new CommandHandler("addNewCategory", info3k));
		MenuItem addNewEventItem = new MenuItem(Info3K.msgs.addNewEvent(), new CommandHandler("addNewEvent", info3k));
		MenuItem addNewLocationItem1 = new MenuItem(Info3K.msgs.addNewLocation(), new CommandHandler("addNewLocation", info3k));
		MenuItem addnewAddressItem1 = new MenuItem(Info3K.msgs.addNewAddress(), new CommandHandler("addNewAddress", info3k));
		
		menuLocations.addItem(locationListItem);
		
		MenuItem myLocations = new MenuItem(Info3K.msgs.myLocations(), new CommandHandler("myLocations", info3k));
		MenuItem myAddresses = new MenuItem(Info3K.msgs.myAddresses(), new CommandHandler("myAddresses", info3k));
		MenuItem myEvents = new MenuItem(Info3K.msgs.myEvents(), new CommandHandler("myEvents", info3k));
		
		final MenuBar myOptions = new MenuBar(true);
		
		if(info3k.getLoginManager().getUser()!=null){
			menuLocations.addItem(addNewLocationItem);
			myOptions.addItem(myAddresses);
			myOptions.addItem(myLocations);
			myOptions.addItem(myEvents);
			myOptions.addSeparator();
			if(UserTypes.ADMINISTRATOR.equals(info3k.getLoginManager().getUser().getUserType())){
				myOptions.addItem(addNewCategoryItem);
			}
			myOptions.addItem(addnewAddressItem1);
			myOptions.addItem(addNewLocationItem1);
			myOptions.addItem(addNewEventItem);
		} else {
			menuLocations.removeItem(addNewLocationItem);
		}
		
		menuAddresses.addItem(addressListItem);
		
		if(info3k.getLoginManager().getUser()!=null){
			menuAddresses.addItem(addnewAddressItem);
		} else {
			menuAddresses.removeItem(addnewAddressItem);
		}
		
		if(info3k.getLoginManager().getUser()!=null){
			menuBar.addItem(Info3K.msgs.myOptions(), true, myOptions);
		}
		
		menuBar.addItem(Info3K.msgs.locations(), true, menuLocations);
		menuBar.addItem(Info3K.msgs.addresses(), true, menuAddresses);
		
		menuBar.addItem(Info3K.msgs.info3KTitle(), true, menuInfo3K);
		
		panel.add(menuBar);
	}

	public void setInfo3k(Info3K info3k) {
		this.info3k = info3k;
	}

	public Info3K getInfo3k() {
		return info3k;
	}
}